package cn.signalpha.modules.shop.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.excel.util.ExcelUtil;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.DateUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.StringPool;
import cn.signalpha.core.tool.utils.StringUtil;
import cn.signalpha.modules.shop.entity.Order;
import cn.signalpha.modules.shop.entity.OrderSettle;
import cn.signalpha.modules.shop.entity.enums.OrderStatusEnum;
import cn.signalpha.modules.shop.excel.OrderSettleExcel;
import cn.signalpha.modules.shop.service.OrderService;
import cn.signalpha.modules.shop.service.OrderSettleService;
import cn.signalpha.modules.shop.vo.OrderSettleVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/order/settle")
@Api(value = "结算单", tags = "结算单")
public class OrderSettleController extends BaseController {

	private final OrderService orderService;
	private final OrderSettleService orderSettleService;

	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="sellerId", dataType = "string", value = "商户id"),
		@ApiImplicitParam(name="orderNo", dataType = "string", value = "订单号"),
		@ApiImplicitParam(name="payNo", dataType = "string", value = "支付单号"),
		@ApiImplicitParam(name="payBizNo", dataType = "string", value = "外部支付单号"),
		@ApiImplicitParam(name="settleWay", dataType = "string", value = "结算方式"),
		@ApiImplicitParam(name="createTimeBegin", dataType = "string", value = "创建开始时间"),
		@ApiImplicitParam(name="createTimeEnd", dataType = "string", value = "创建结束时间"),
		@ApiImplicitParam(name="settleTimeBegin", dataType = "string", value = "结算开始时间"),
		@ApiImplicitParam(name="settleTimeEnd", dataType = "string", value = "结算结束时间"),
	})
	@ApiOperation(value = "分页", notes = "")
	public R<IPage<OrderSettleVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("payStatus", "1");
		return R.data(orderSettleService.selectOrderSettlePage(Condition.getPage(query), params));
	}

	@GetMapping("/total")
	@ApiOperation(value = "统计", notes = "")
	public R<Map<String, Object>> total(@ApiIgnore @RequestParam Map<String, Object> params) {
		Map<String, Object> dataMap = new HashMap<>(4);
		dataMap.put("data", orderSettleService.selectOrderSettleTotal(Kv.create().setAll(params).set("settleStatus", "")));
		dataMap.put("data0", orderSettleService.selectOrderSettleTotal(Kv.create().setAll(params).set("settleStatus", "0")));
		dataMap.put("data1", orderSettleService.selectOrderSettleTotal(Kv.create().setAll(params).set("settleStatus", "1")));
		return R.data(dataMap);
	}

	@GetMapping("/excel")
	@ApiOperation(value = "excel", notes = "")
	public void excel(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		params.put("payStatus", "1");
		List<OrderSettleExcel> orderExcelList = orderSettleService.selectOrderSettleExcel(params);
		ExcelUtil.export(response, DateUtil.time(), StringPool.EMPTY, orderExcelList, OrderSettleExcel.class);
	}

	@PostMapping("/clearing")
	@ApiOperation(value = "手动结算", notes = "传入ids")
	public R clearing(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		Func.toLongList(ids).forEach(id -> {
			OrderSettle orderSettle = orderSettleService.getById(id);
			Assert.isNull(orderSettle, MsgConstant.OBJECT_NULL);
			Assert.isFalse(orderSettle.getSettleStatus() == 1,  StringUtil.format("订单[{}]已结算，请不要重复结算", orderSettle.getOrderNo()));
			Order order = orderService.getByOrderNo(orderSettle.getOrderNo());
			Assert.isTrue(order.getOrderStatus().equals(OrderStatusEnum.SUCCESS.getStatus()), StringUtil.format("订单[{}]状态未完成不可结算", orderSettle.getOrderNo()));
			Assert.isTrue(order.getRefundStatus() == 0, StringUtil.format("订单[{}]存在售后，不可结算", orderSettle.getOrderNo()));
			orderSettleService.settleOrder(orderSettle.getId());
		});
		return R.status(true);
	}

}
